package org.kinglone.index.action;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.kinglone.back.article.model.Article;
import org.kinglone.back.friendlinks.service.FriendlinksService;
import org.kinglone.back.article.service.ArticleService;
import org.kinglone.back.classify.model.Classify;
import org.kinglone.back.classify.service.ClassifyService;
import org.kinglone.back.comment.model.Comment;
import org.kinglone.back.comment.service.CommentService;
import org.kinglone.back.music.service.MusicService;
import org.kinglone.common.pager.PageModel;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import java.math.BigInteger;
import java.util.List;

@Controller
public class IndexAction {

    @Resource(name="articleService")
    private ArticleService articleService;

    @Resource(name="musicService")
    private MusicService musicService;

    @Resource(name="friendlinksService")
    private FriendlinksService friendlinksService;
    
    @Resource(name="commentService")
    private CommentService commentService;
    
    @Resource(name="classifyService")
	private ClassifyService classifyService;

    /**
     * 跳转到首页
     * @param request
     * @return
     */
    @RequestMapping("/index.jspx")
    public ModelAndView frontIndex(HttpServletRequest request,PageModel pageModel){
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/jsp/front/indexmain.jsp");

        String classifyId = request.getParameter("classifyId");
        String title = request.getParameter("title");
        System.out.println("================================搜索的标题为："+title);

        List<Article> articleList = articleService.findByPage(pageModel,classifyId,title);
        JSONArray jsonArray = new JSONArray();
        for (int i = 0; i < articleList.size(); i++) {
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("sysid", articleList.get(i).getSysid());
			jsonObject.put("title", articleList.get(i).getTitle());
			jsonObject.put("briefing", articleList.get(i).getBriefing());
			jsonObject.put("publishDatetime", articleList.get(i).getPublishDatetime());
			jsonObject.put("commentCount", commentService.getCountByArticleId(articleList.get(i).getSysid()));
			jsonArray.add(jsonObject);
		}
        request.setAttribute("jsonArray", jsonArray);

        return  modelAndView;
    }

    /**
     * 跳转到文章明细
     * @param request
     * @return
     */
    @RequestMapping("/article/show.jspx")
    public ModelAndView show(HttpServletRequest request){
        ModelAndView modelAndView = new ModelAndView("/WEB-INF/jsp/front/show.jsp");

        String sysid = request.getParameter("id");
        Article article = articleService.get(sysid);
        request.setAttribute("article",article);
        
        //上一篇文章
        Article upperArticle = articleService.findUpperArticleById(article.getPublishDatetime());
        request.setAttribute("upperArticle", upperArticle);
        //下一篇文章        
        Article nextArticle = articleService.findNextArticleById(article.getPublishDatetime());
        request.setAttribute("nextArticle", nextArticle);

        if(article.getClassifyId() != null){
            Classify classify = classifyService.get(article.getClassifyId());
            if(classify != null){
                request.setAttribute("classifyName", classify.getClassifyName());
                request.setAttribute("classifyId", classify.getSysid());
            }else{
                request.setAttribute("classifyName","");
            }
        }else{
            request.setAttribute("classifyName","");
        }

        List<Comment> commentList = commentService.getByArticleId(sysid);
        request.setAttribute("commentList", commentList);


        return  modelAndView;
    }

}
